//
//  BKOrderPaymentView.m
//  bestkeep
//
//  Created by 马乾征 on 16/11/14.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKOrderPaymentView.h"
#import "BK_OD_Color.h"
/**
    基类
 */
@implementation BKOrderPaymentView

@end


/**
    支付时间限制
 */
@implementation BKOrderPaymentTimeLimitTableHeaderView

{
    UILabel *_timeLimit;
    NSTimer *_limitTimer;
    NSInteger _timeInteger;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    _timeLimit = [[UILabel alloc] init];
    _timeLimit.backgroundColor = [UIColor clearColor];
    _timeLimit.textColor = kOrderLightGrey1;
    if (ScreenHeight<600) {
        _timeLimit.font = UIFontCustom(11);
    }else{
        _timeLimit.font = UIFontCustom(13);
    }

    [self addSubview:_timeLimit];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_timeLimit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ApplicationBackGauge);
        make.centerY.mas_equalTo(self);
    }];
}

- (void)setTimeLimit:(NSNumber *)timeLimit
{
    
    if (_limitTimer) {
        [self timerInvalidate];
    }
    
    _timeInteger = [timeLimit integerValue];
//    _limitTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeToChange) userInfo:nil repeats:YES];
    
    /*
     * 防止页面滑动计时器不走动
     */
    _limitTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeToChange) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_limitTimer forMode:NSRunLoopCommonModes];
    self.backgroundColor = kOrderCream_colored;
    //给一次初始时间
    NSString *time = [self TimeTransformation:_timeInteger];
    _timeLimit.attributedText = [self syntheticAttributedString:time];
}


- (void)timeToChange
{
    _timeInteger--;
    if (_timeInteger < 0)
    {
        [self timerInvalidate];
        _timeLimit.text = @"此订单已关闭";
        if (self.delegate && [self.delegate respondsToSelector:@selector(orderPaymentTimeOut)]) {
            [self.delegate orderPaymentTimeOut];
        }
        return;
    }
    NSString *time = [self TimeTransformation:_timeInteger];
    _timeLimit.attributedText = [self syntheticAttributedString:time];
    
}

- (NSString *)TimeTransformation:(NSUInteger )time_int{
    unsigned long minutes = time_int / 60;
    unsigned long seconds = time_int % 60;
    NSString *showTime;
    if (minutes) {
        showTime = [NSString stringWithFormat:@"%lu分钟%lu秒", minutes, seconds];
    }
    else{
        showTime = [NSString stringWithFormat:@"%lu秒", seconds];
    }
    return showTime;
}

- (NSMutableAttributedString *)syntheticAttributedString:(NSString *)string
{
    NSString *leftStr = @"此订单将在";
    NSString *rightStr = @"后自动关闭，请及时完成付款！";
    
    NSString *preliminaryString = [NSString stringWithFormat:@"%@%@%@", leftStr, string, rightStr];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:preliminaryString];
    [attributedString addAttribute:NSForegroundColorAttributeName value:kOrderLightGrey1 range:NSMakeRange(0, leftStr.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:kOrderPriceRed range:NSMakeRange(leftStr.length, string.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:kOrderLightGrey1 range:NSMakeRange(preliminaryString.length - rightStr.length, rightStr.length)];
    
    return attributedString;
}

- (void)removeFromSuperview
{
    [self timerInvalidate];
    [super removeFromSuperview];
}

- (void)timerInvalidate
{
    [_limitTimer invalidate];
    _limitTimer = nil;
}

@end





/**
    底部 立即支付
 */
@implementation BKOrderPaymentNowView
{
    UILabel *_realPayText;
    UILabel *_realPayAmount;
    UIButton *_paymentNow;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor =[UIColor whiteColor];
    
    /*
     * BKOrderPayRealPay   实付
     */
    _realPayText = [[UILabel alloc] init];
    _realPayText.backgroundColor = [UIColor clearColor];
    _realPayText.textColor = kOrderBlack1;
    _realPayText.font = UIFontCustom(15);
    _realPayText.text = kOrderLocalizeString(@"BKOrderPayRealPay");
    [self addSubview:_realPayText];
    
    /*
     * 金额
     */
    _realPayAmount = [[UILabel alloc] init];
    _realPayAmount.backgroundColor = [UIColor clearColor];
    if (ScreenHeight<600) {
        _realPayAmount.font = UIFontCustom(18);
    }else{
        _realPayAmount.font = UIFontCustom(21);
    }
    
    _realPayAmount.textColor = [UIColor redColor];
    [self addSubview:_realPayAmount];
    
    _paymentNow = [[UIButton alloc] init];
    [_paymentNow setTitleColor:kOrderWhite forState:UIControlStateNormal];
    _paymentNow.titleLabel.font = UIFontCustom(18);
    _paymentNow.titleLabel.textAlignment = NSTextAlignmentCenter;
    UIImage *image = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:kOrderBKGreen];
    [_paymentNow setBackgroundImage:image forState:UIControlStateNormal];
    UIImage *disabledImage = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:[UIColor grayColor]];
    [_paymentNow setBackgroundImage:disabledImage forState:UIControlStateDisabled];
    [_paymentNow setTitle:kOrderLocalizeString(@"BKOrderPayNow") forState:UIControlStateNormal];
    [_paymentNow addTarget:self action:@selector(payNowAction:) forControlEvents:UIControlEventTouchUpInside];
    [self setCanPayment:NO];
    [self addSubview:_paymentNow];
    
    [self makeConstraints];
}

- (void)payNowAction:(UIButton *)sender
{
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKOrderPaymentNowViewDelegate)])
    {
        [self.delegate orderPaymentNowViewDidSelected:self];
    }
}

- (void)makeConstraints
{
    CGSize textSize = [kOrderLocalizeString(@"BKOrderPayRealPay") sizeWithAttributes:@{NSFontAttributeName : UIFontBoldCustom(16)}];
    /*
     * BKOrderPayRealPay   实付
     */
    [_realPayText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ApplicationBackGauge);
        make.centerY.mas_equalTo(self);
        make.size.mas_equalTo(textSize);
    }];
    
    /*
     * 金额
     */
    [_realPayAmount mas_makeConstraints:^(MASConstraintMaker *make) {
        if (ScreenHeight<600) {
            make.left.mas_equalTo(_realPayText.mas_right).offset(8);
        }else{
            make.left.mas_equalTo(_realPayText.mas_right).offset(15);
        }
        
        make.centerY.mas_equalTo(self);
        make.right.mas_equalTo(self.mas_centerX).offset(-10);
    }];
    
    [_paymentNow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.mas_centerX);
    }];

}

- (void)setPaymentWithAmount:(NSNumber *)amount{
    _amount = amount;
    _realPayAmount.text = [NSString stringWithFormat:@"￥%.2f",[amount floatValue]];
}

- (void)setCanPayment:(BOOL)canPayment
{
    if (canPayment) {
        _paymentNow.enabled = YES;
    }
    else
    {
        _paymentNow.enabled = NO;
    }
}

@end
